package com.tekj.cxqcb.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import common.base.MainApplication;
import es.dmoral.toasty.Toasty;

/**
 * description: 微信支付回调
 * author: tongzhenggang@gmail.com
 * date: 2018/3/9
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
    private static final String APP_ID = MainApplication.APP_ID;// "wxb3bcf8336cf8da5d";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        Toast.makeText(this, "openid = " + req.openId, Toast.LENGTH_SHORT).show();

        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                Toast.makeText(this, "  COMMAND_GETMESSAGE_FROM_WX", Toast.LENGTH_SHORT).show();

                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                Toast.makeText(this, "  COMMAND_SHOWMESSAGE_FROM_WX", Toast.LENGTH_SHORT).show();

                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
                Toast.makeText(this, "Launch From Weixin", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        Log.d("onResp", "onPayFinish, errCode = " + resp.errCode);

        if (resp.errCode == 0) {//支付成功
//            Intent intent = new Intent();
//            intent.setAction("fbPayAction");
////          intent.setAction("goodsPayAction");
////          intent.setAction("integraPayAction");
//            sendBroadcast(intent);

//            startActivity(new Intent(getApplicationContext(), WeixinPaysucceedActivity.class));//支付成功页面

//            Toast.makeText(getApplicationContext(), "支付成功", Toast.LENGTH_LONG).show();

            Toasty.success(getApplicationContext(), "支付成功").show();

//            MainActivity.mainActivity.finish();
//            Intent i = new Intent(this, MainActivity.class);
//            i.putExtra("open", 2);//我的界面
//            startActivity(i);

            finish();
            EventBus.getDefault().post(new EventBusMsgBean(EventBusMsgBean.EnumStatus.微信支付成功, "finish"));
        } else if (resp.errCode == -1) {//支付失败

//            Toast.makeText(getApplicationContext(), "支付失败：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。", Toast.LENGTH_LONG).show();
            Toasty.error(getApplicationContext(), "支付失败：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。").show();
            finish();
        } else if (resp.errCode == -2) {//取消
//            Toast.makeText(getApplicationContext(), "支付取消", Toast.LENGTH_LONG).show();

            Toasty.info(getApplicationContext(), "支付取消").show();
            finish();
        }


    }

}